<template>
  <div class="goods">
    <div class="wrap">
      <Crumb :data="nav"> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="option1">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: corecheck == index }"
            v-for="(item, index) in option1"
            :key="index"
            @click="option1fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: item.isActive }"
            v-for="(item, index) in option2"
            :key="index"
            @click="option2fn(index, item.type)"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="lislimit" />
      <div class="loading">
        <div v-show="Showloading">
          <van-loading size="24px">加载中...</van-loading>
        </div>
        <div v-show="!Showloading" style="color: gray">已经到底了~</div>
      </div>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { goodsApi } from "../request/api";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";

export default {
  data() {
    return {
      lis: [],
      // 默认显示8条数据
      lislimit: [],
      nav: [
        { name: "首页", link: "#" },
        { name: "全部商品", link: "#" },
      ],
      option1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      option2: [
        { txt: "全部", type: 0, isActive: true },
        { txt: "实物礼品", type: 1, isActive: false },
        { txt: "虚拟礼品", type: 2, isActive: false },
      ],
      // 用于判断那个高亮
      corecheck: 0,
      typecheck: 0,
      // 请求参数
      did: 0,
      type: 0,
      min: 0,
      max: 0,
      keyword: "",
      // 当前页
      num: 1,
      // 单页数量
      size: 8,
      // 是否加载中,用于滚动节流
      loading: false,
      // 显示加载状态
      Showloading: true,
    };
  },
  components: {
    Crumb,
    List,
  },
  async created() {
    this.keyword = this.$route.query.keyword;
    this.getgoods();
    document.documentElement.scrollTop = 0;
  },

  methods: {
    option1fn(index, min, max) {
      this.corecheck = index;
      this.min = min;
      this.max = max;
      this.getgoods();
    },
    option2fn(index, type) {
      // 排他
      this.option2.map((item) => (item.isActive = false));
      this.option2[index].isActive = true;
      this.type = type;
      this.getgoods();
    },

    // 封装请求
    async getgoods() {
      let res = await goodsApi({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      // console.log(res);
      this.lis = res.data;
      this.lislimit = this.lis.filter((item, index) => index < 8);
      // 初始化
      this.num = 1;
      this.Showloading = true;
      if (this.lis.length == this.lislimit.length) {
        this.Showloading = false;
        return;
      }
    },

    // 封装滚动加载函数
    getscroll() {
      if (getScrollTop() + getClientHeight() > getScrollHeight() - 200) {
        if (this.lis.length == this.lislimit.length) {
          this.Showloading = false;
          return;
        }
        // 使用节流控制加载次数
        if (this.loading == false) {
          this.loading = true;
          this.num++;
          // 这里只是模拟加载时的状态
          setTimeout(() => {
            for (
              var i = (this.num - 1) * this.size;
              i < this.num * this.size;
              i++
            ) {
              // 处理数据总量超出问题
              this.lis[i] && this.lislimit.push(this.lis[i]);
            }
            this.loading = false;
          }, 500);
        }
      }
    },
  },
  watch: {
    "$route.query": {
      handler(n, o) {
        this.keyword = n.keyword;
        this.getgoods();
      },
    },
  },
  mounted() {
    window.addEventListener("scroll", this.getscroll);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.getscroll);
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/style.less";
@base-color: red;

.loading {
  text-align: center;
  margin-top: 10px;
}

.goods {
  padding-bottom: 30px;
  background: #efefef;
  padding-top: 20px;
  .option1 {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>